*Main Text Replication file for "Global Finance, Political Business Cycles, and the Politics of Foreign Reserves"
*Ben Cormier and Patrick Shea
*Analysis run in Stata 15
*Data sources cited in text

*Packages:
*ssc install estout
*ssc install ebalance
*ssc install interflex


use "data1_final.dta", clear


***************************************************************************************************************************************************************************
*Data prep
tsset country_num year
*Set color scheme for plots
set scheme s1mono
grstyle init
grstyle set plain, nogrid 

*Subset to developing
keep if oecd_1yes == 0
*Subset to VDem democracy level
keep if v2x_polyarchy > .42



***************************************************************************************************************************************************************************
*Table 1


regress deltaReserves_Imports ElectionPeriod ElectionYear US10YrYields_AnnAvg l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num  c.year##c.GlobalReserveLevels, robust
estimates store m1

regress deltaReserves_Imports c.ElectionPeriod##c.US10YrYields_AnnAvg i.country_num, robust
estimates store m2

regress deltaReserves_Imports c.ElectionPeriod##c.US10YrYields_AnnAvg ElectionYear l.deltaReserves_Imports i.country_num c.year##c.GlobalReserveLevels, robust
estimates store m3

regress deltaReserves_Imports c.ElectionPeriod##c.US10YrYields_AnnAvg ElectionYear l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
estimates store m4




lab var US10YrYields_AnnAvg "USrates"
lab var CurrAcctBal_PerGDP  "CurrAcct"
lab var lvaw_garriga "CBI"
lab var Taxes_PerGDP "TaxRevenue"
lab var ny_gdp_totl_rt_zs "NatRents"
lab var cyrs "CCrisisYrs"
lab var dyrs "DCrisisYrs"
lab var BestCRtg "CreditRating"
lab var v2x_polyarchy "Democracy"
lab var DebtService_PerGDP "DebtService"
lab var Inflation_annGDPdeflator "Inflation"
lab var ExtDebtStocks_PerGNI "ExtDebt"
lab var PegBandManaged_NoUnion "PegBand"
lab var deltaReserves_Imports "Reserves"
lab var year "Year"
lab var GlobalReserveLevels "GlobalReserves"
lab var ElectionYear "ElectionYear"




esttab m1 m2 m3 m4, ///
cells(b(star fmt(%9.3f)) se(par)) stats(N , fmt(%9.2f %9.0f)) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend ///
title("Table 1: Models of Reserve Levels") ///
note("Robust Standard Errors")	///
 label collabels(none) ///
    varlabels(_cons Constant "c.ElectionPeriod#c.US10YrYields_AnnAvg" "Election*USrates" "c.year#c.GlobalReserveLevels" "Year*GlobalReserves") ///
    order(ElectionPeriod US10YrYields_AnnAvg c.ElectionPeriod#c.US10YrYields_AnnAvg ElectionYear) drop(*country_num c.year#c.GlobalReserveLevels GlobalReserveLevels year) 




	
***************************************************************************************************************************************************************************
*Table 2

* Generate pre-election share
replace  monthDPI= . if ElectionPeriod==0 
gen preelec_share = 0 if year <=2020
replace preelec_share = monthDPI/12 if monthDPI != . 
by country_num, sort: replace preelec_share = (12-monthDPI[_n+1])/12 if monthDPI[_n+1] != .

* Make Hybrid measure
gen preelec_share2 = preelec_share 
replace preelec_share2 = 1 if F.ElectionPeriod==1 & ElectionPeriod==1 & year <=2020	


* Generate post-election share (monthly) 
gen postelec_share = 0 if year <=2020
replace postelec_share = (12-monthDPI)/12 if monthDPI != .

* Generate post-election share (annual) 
gen post_Election =0 if year <=2020
replace post_Election =1 if ElectionLastYear==1 

	
regress deltaReserves_Imports c.preelec_share c.US10YrYields_AnnAvg postelec_share l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
estimates store m5

regress deltaReserves_Imports c.preelec_share##c.US10YrYields_AnnAvg postelec_share l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
estimates store m6

regress deltaReserves_Imports c.preelec_share2 c.US10YrYields_AnnAvg postelec_share l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
estimates store m7

regress deltaReserves_Imports c.preelec_share2##c.US10YrYields_AnnAvg postelec_share l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
estimates store m8

lab var preelec_share "ElectionMonths"
lab var preelec_share2 "ElectionHybrid"
lab var postelec_share "PostElectionMonths"


*No controls shown in table output, but are present in model.


esttab m5 m6 m7 m8, ///
cells(b(star fmt(%9.3f)) se(par)) stats(N , fmt(%9.2f %9.0f)) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend ///
    title("Table 2: Models of Reserve Levels with Monthly Elections") ///
    note("Robust Standard Errors") ///
    label collabels(none) ///
    order(US10YrYields_AnnAvg preelec_share c.preelec_share#c.US10YrYields_AnnAvg ///
          preelec_share2 c.preelec_share2#c.US10YrYields_AnnAvg postelec_share postelec_share L.deltaReserves_Imports) ///
    keep(US10YrYields_AnnAvg preelec_share c.preelec_share#c.US10YrYields_AnnAvg ///
         preelec_share2 c.preelec_share2#c.US10YrYields_AnnAvg postelec_share postelec_share L.deltaReserves_Imports) 


	
************************************************************************************************************************************************************
*Marginal Effects

*Figure 1: Model 4 Adj ME Plot
regress deltaReserves_Imports c.ElectionPeriod##c.US10YrYields_AnnAvg ElectionYear l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
margins, dydx(ElectionPeriod) at(US10YrYields_AnnAvg=(.8(.25)6.0)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(1(1)5) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("Figure 1: Predicted Effect of Election Periods on Reserve Levels (Model 4)", size (mediumsmall)) subtitle("by US Interest Rates", size (small)) ///
note("95% confidence intervals", size(small)) ///
ytitle("Marginal Effect of Election on Reserves") xtitle("US Rates")

*Figure 5: Model 6 Adj ME Plot
regress deltaReserves_Imports c.preelec_share##c.US10YrYields_AnnAvg postelec_share l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
margins, dydx(preelec_share) at(US10YrYields_AnnAvg=(.8(.25)6.0)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(1(1)5) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("Figure 5: Predicted Effect of Election Periods on Reserve Levels (Monthly Measure, Model 6)", size (small)) subtitle("by US Interest Rates", size (small)) ///
note("95% confidence intervals", size(small)) ///
ytitle("Marginal Effect of Election on Reserves") xtitle("US Rates")

*Figure 7: Model 8 Adj ME Plot
regress deltaReserves_Imports c.preelec_share2##c.US10YrYields_AnnAvg postelec_share l.deltaReserves_Imports l.GDPpcap l.GDPgrowth l.ny_gdp_totl_rt_zs l.CurrAcctBal_PerGDP l.ExtDebtStocks_PerGNI l.DebtService_PerGDP l.Inflation_annGDPdeflator l.PegBandManaged_NoUnion l.lvaw_garriga l.Taxes_PerGDP l.BestCRtg l.cyrs l.dyrs l.v2x_polyarchy l.Left i.country_num c.year##c.GlobalReserveLevels, robust
margins, dydx(preelec_share2) at(US10YrYields_AnnAvg=(.8(.25)6.0)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(1(1)5) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("Figure 7: Predicted Effect of Election Periods on Reserve Levels (Hybrid Measure, Model 8)", size (small)) subtitle("by US Interest Rates", size (small)) ///
note("95% confidence intervals", size(small)) ///
ytitle("Marginal Effect of Election on Reserves") xtitle("US Rates")


************************************************************************************************************************************************************
*Kernel estimates
*prep variables for interflex
*country and year dummies
tab country_num , g(cc_)
tab year, g(yr_)
*LDV
gen ldeltaReserves_Imports = L.deltaReserves_Imports
*Manual year trends
gen yearGR = year*GlobalReserveLevels

global trends yearGR year GlobalReserveLevels

*Controls
global cntall GDPpcap GDPgrowth ny_gdp_totl_rt_zs CurrAcctBal_PerGDP ExtDebtStocks_PerGNI DebtService_PerGDP Inflation_annGDPdeflator PegBandManaged_NoUnion lvaw_garriga Taxes_PerGDP BestCRtg cyrs dyrs v2x_polyarchy Left

foreach var in GDPpcap GDPgrowth ny_gdp_totl_rt_zs CurrAcctBal_PerGDP ExtDebtStocks_PerGNI DebtService_PerGDP Inflation_annGDPdeflator PegBandManaged_NoUnion lvaw_garriga Taxes_PerGDP BestCRtg cyrs dyrs v2x_polyarchy Left {
	gen lagx_`var' = l.`var'
	}
	

*Figure 2: Kernel estimates
interflex deltaReserves_Imports  ElectionPeriod US10YrYields_AnnAvg ldeltaReserves_Imports $trends ElectionYear lagx_* cc_*, vce(robust) bw(1.6) type(kernel) ylabel("Reserves") dlabel("Election")  xlabel("US Rates")	///
title("Figure 2: Kernel Estimates of Conditional Effects")


*Figure 3: Bins
interflex deltaReserves_Imports  ElectionPeriod US10YrYields_AnnAvg ldeltaReserves_Imports $trends ElectionYear  lagx_* cc_*, nbins(4) vce(robust) ylabel("Reserves") dlabel("Election")  xlabel("US Rates") ///
title("Figure 3: Binning the Conditional Effects")
 
 
*Figure 6: Election Share (model 6)
interflex deltaReserves_Imports  preelec_share US10YrYields_AnnAvg ldeltaReserves_Imports $trends postelec_share lagx_* cc_*, vce(robust) bw(1.6) type(kernel) ylabel("Reserves") dlabel("Election Share")  xlabel("US Rates")	///
title("Figure 6: Kernel Estimates (Monthly Measure, Model 6)")
 
 
*Figure 8: Election Hybrid Share (model 8)
interflex deltaReserves_Imports  preelec_share2 US10YrYields_AnnAvg ldeltaReserves_Imports $trends postelec_share lagx_* cc_*, vce(robust) bw(1.6) type(kernel) ylabel("Reserves") dlabel("Election Hybrid Share")  xlabel("US Rates")	///
title("Figure 8: Kernel Estimates (Hybrid Measure, Model 8)")
 
 
************************************************************************************************************************************************************
*Figure 4 High vs Low Yields
 
*Split by above and below 3
gen hirates = 0 if 	US10YrYields_AnnAvg !=. 
replace hirates  = 1 if  US10YrYields_AnnAvg >3 &  US10YrYields_AnnAvg  !=. 

regress deltaReserves_Imports i.ElectionPeriod##i.hirates ElectionYear l.(deltaReserves_Imports GDPpcap GDPgrowth ny_gdp_totl_rt_zs CurrAcctBal_PerGDP ExtDebtStocks_PerGNI DebtService_PerGDP Inflation_annGDPdeflator PegBandManaged_NoUnion lvaw_garriga Taxes_PerGDP BestCRtg cyrs dyrs v2x_polyarchy Left) i.country_num c.year##c.GlobalReserveLevels, robust


margins, dydx(ElectionPeriod) at(hirates=(0 1)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) ///
    xscale(lstyle(none)) xlabel(-.25 " " 0 "Low" 1 "High" 1.25 " ", notick) ///
    recast(scatter) ///
    plot1opts(msymbol(O) mcolor(black)) ///
    recastci(rcap) ///
    ciopts(lc(black%80)) ///
title("Figure 4: Predicted Effect of Election Periods on Reserve Levels", size (mediumsmall)) subtitle("by US Low and High Treasury Yields", size (small)) ///
note("95% confidence intervals", size(small)) ///
ytitle("Change in Reserves") xtitle("US Yields")


************************************************************************************************************************************************************
*Figure 9 Split Sample by reserves thresholds

**Reserves more than 6 months
regress deltaReserves_Imports i.ElectionPeriod##c.US10YrYields_AnnAvg  l.deltaReserves_Imports  i.country_num c.year##c.GlobalReserveLevels if l.Reserves_MonthsOfImports >=6 & Reserves_MonthsOfImports!=., robust
margins, dydx(ElectionPeriod) at(US10YrYields_AnnAvg=(1.75(.25)5)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel( 2(1)5,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("", size (mediumsmall)) subtitle("Reserves are High", size (small)) ///
note("95% confidence intervals", size(small)) ///
ytitle("Change in Reserves") xtitle("US Rates") name(hi, replace)


**Reserves less than 6 months
regress deltaReserves_Imports i.ElectionPeriod##c.US10YrYields_AnnAvg  l.deltaReserves_Imports  i.country_num c.year##c.GlobalReserveLevels if l.Reserves_MonthsOfImports <6, robust
margins, dydx(ElectionPeriod) at(US10YrYields_AnnAvg=(1.75(.25)5)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel( 2(1)5,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("", size (mediumsmall)) subtitle("Reserves are Low", size (small)) ///
note("95% confidence intervals", size(small)) ///
ytitle("Change in Reserves") xtitle("USRates")  name(lo, replace)


graph combine hi lo, ycommon title("Figure 9: Predicted Effect of Election Periods", size (mediumsmall))
 
